// pages/choose_colors/model.js
let videoIsFullScreen = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemColor: '',
    uploadUrl: wx.getStorageSync('uploadUrl'),
    currentItem: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on && eventChannel.on('colorItem', data => {
      this.setData({
        itemColor: data,
        modelJson: data.modelJson && data.modelJson != '' ? (typeof data.modelJson == "string" ? JSON.parse(data.modelJson) : data.modelJson) : [],
        videoJson: data.videoJson && data.videoJson != '' ? (typeof data.videoJson == "string" ? JSON.parse(data.videoJson) : data.videoJson) : []
      })
    })
    
    eventChannel.on && eventChannel.on('currentItem', data => {
      console.log('data',data)
      this.setData({
        currentItem: data
      })
    })

    console.log('itemColor', this.data.itemColor)
    console.log('modelJson', this.data.modelJson)
    console.log('videoJson', this.data.videoJson)
    console.log('currentItem', this.data.currentItem)
  },

  toggleFullScreen(e) {
    let videoContext = wx.createVideoContext('videoEle', this);
    if (videoIsFullScreen) {
      // videoContext.exitFullScreen();
      // videoIsFullScreen = false;
    } else {
      videoContext.requestFullScreen({
        direction: 0
      });
      videoContext.play();
      videoIsFullScreen = true;
    }
  },

  onFullScreenChange(event) {
    console.log(event);
    if(!event.detail.fullScreen) {
      let videoContext = wx.createVideoContext('videoEle', this);
      videoContext.exitFullScreen();
      videoIsFullScreen = false; 
    }
  },

  setFullScreen(e) {
    let videoContext = wx.createVideoContext('videoEle', this);
    videoContext.requestFullScreen({
      direction: 0
    });
    videoIsFullScreen = true;
  },

  exitFullScreen(e) {
    let videoContext = wx.createVideoContext('videoEle', this);
    videoContext.exitFullScreen();
    videoIsFullScreen = false;
  },

  toColor: function (e) {
    var itemColor = e.currentTarget.dataset.item
    this.setData({
      itemColor
    }, () => {
      wx.getSetting({
        withSubscriptions: true,
        success (res) {
          console.log(res)
          if (!res.authSetting['scope.bluetooth']) {
            console.log(res.authSetting, 111)
            wx.authorize({
              scope: 'scope.bluetooth',
              success () {
                wx.navigateTo({
                  url: '/pages/colors_project/index',
                  success: (res) => {
                    res.eventChannel.emit('colorItem', this.data.itemColor)
                  }
                })
              }
            })
          } else {
            wx.navigateTo({
              url: '/pages/colors_project/index',
              success: (res) => {
                res.eventChannel.emit('colorItem', this.data.itemColor)
              }
            })
          }
        }
      })
      
      // wx.navigateTo({
      //   url: '/pages/dye_paste/operation',
      //   success: (res) => {
      //     res.eventChannel.emit('colorItem', itemColor)
      //   }
      // })
    })

  },
})